<template>
  <div class='container'>
    <el-form
    :model="ruleForm"
     :rules="rules"
      ref="ruleForm"
       label-width="80px">
  <el-form-item label="学科所属" prop="subjectName" v-if="!subjectID">
     <el-select v-model="value" placeholder="请选择" style="width:100%;">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
  </el-form-item>
 <el-form-item label="标签名称" prop="tagName">
    <el-input v-model="ruleForm.tagName" placeholder="请输入标签名"></el-input>
  </el-form-item>
   <el-form-item style="float:right;padding-top:30px" >
        <el-button @click="succeed()">取 消</el-button>
    <el-button type="primary" @click="submitForm(isEdit)">确 定</el-button>
  </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { add, detail, update } from '@/api/hmmm/tags'
import { simple } from '@/api/hmmm/subjects'
export default {
  props: {
    isEdit: {
      type: Boolean,
      required: true
    },
    id: {
      type: Number,
      default: null
    },
    subjectID: {
      type: Number,
      default: null
    }
  },
  data () {
    return {
      ruleForm: {
        id: null,
        subjectID: '',
        tagName: ''
      },
      rules: {
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ]
      },
      // delivery: true,
      options: [{
        value: '1',
        label: ' 已启用 '
      }, {
        value: '0',
        label: ' 已禁用 '
      }],
      value: ''

    }
  },
  async created () {
    // console.log(this.subjectID)
    // if(this.subjectId)
    this.getsimple()
    // console.log('this.id', this.id)
    if (this.isEdit) {
      this.ruleForm.id = this.id
      const { data: res } = await detail({ id: this.ruleForm.id })
      // console.log(res)
      this.ruleForm = res
      this.value = res.subjectID
    }
  },
  methods: {
    // 获取学科所属
    async getsimple () {
      const res = await simple()
      // console.log(res)
      this.options = res.data
    },
    submitForm () {
      // 兜底校验
      this.$refs.ruleForm.validate(reslut => {
        // console.log('reslut', reslut)
        if (!reslut) return
        // if (this.delivery) {
        //   this.ruleForm.isFrontDisplay = 1
        // } else {
        //   this.ruleForm.isFrontDisplay = 0
        // }
        if (this.subjectID) {
          this.ruleForm.subjectID = this.subjectID
        } else {
          this.ruleForm.subjectID = this.value
        }
        if (!this.isEdit) {
          this.addSub()
        } else {
          this.delSub()
        }
      })
    },
    addSub () {
      // console.log('this.ruleForm.isFrontDisplay', this.ruleForm.isFrontDisplay)
      add(this.ruleForm).then(response => {
        // console.log('1234', response)
        this.$message.success('新增标签成功!')
        this.succeed()
      }).catch(response => {
        this.$message.error('新增失败!')
      })
      // this.$message.info('已取消操作!')
    },
    delSub () {
      update(this.ruleForm).then(response => {
        // console.log('1234', response)
        this.$message.success('修改标签成功!')
        this.succeed()
      }).catch(response => {
        this.$message.error('修改失败!')
      })
    },
    succeed () {
      this.ruleForm = {
        subjectName: '',
        isFrontDisplay: 0
      }
      this.$emit('success')
    }

  }
}
</script>

<style scoped lang='scss'>
  .container {
    height: 180px;
  }
</style>
